package com.dmp.common.string;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @ClassName StringSplitUtil
 * @Description
 *
 * @author sunzy
 * @date 2016年2月29日 下午12:48:08
 * @version 1.0
 *
 */
public class StringSplitUtil {
	/** 对象字符串的分隔符 */
	private static String splitStr = null;
	/** String对象字符串的分隔后的key组 */
	private static String splitKey = null;
	/** String[]对象字符串的分隔后的key组 */
	private static String[] keys = null;

	public static StringSplitData getStringSplitData(String str) {
		StringSplitData strSplitData = new StringSplitData();
		try {
			if (null == StringSplitUtil.keys) {
				StringSplitUtil.keys = StringSplitUtil.splitKey.split(",");
			}
			String[] strValues = str.split(StringSplitUtil.splitStr);

			for (int i = 0; i < StringSplitUtil.keys.length; i++) {
				String strKey = StringSplitUtil.keys[i];
				String value = strValues[i];

				String methodName = "set"
						+ strKey.substring(0, 1).toUpperCase()
						+ strKey.substring(1);

				
				Method meth = strSplitData.getClass().getMethod(methodName, String.class);
				meth.invoke(strSplitData, value);
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (SecurityException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			System.exit(-1);
		}

		return strSplitData;
	}

	public static String getSplitStr() {
		return StringSplitUtil.splitStr;
	}

	public static void setSplitStr(String splitStr) {
		StringSplitUtil.splitStr = splitStr;
	}

	public static String getSplitKey() {
		return StringSplitUtil.splitKey;
	}

	public static void setSplitKey(String splitKey) {
		StringSplitUtil.splitKey = splitKey;
	}
}
